﻿
//###{"class":"go.GraphLinksModel","nodeDataArray":[{"text":"stateA","isInit":true,"key":-1,"loc":"444 227.00000000000006","callbacks":"{\"enter\":[\"enterA\",\"enterA2\"],\"leave\":[\"leaveA\",\"leaveA2\"]}"},{"text":"stateB","isInit":false,"key":-2,"loc":"282 417","callbacks":"{\"enter\":[\"enterB\"],\"leave\":[\"leaveB\"]}"}],"linkDataArray":[{"from":-1,"to":-2,"points":[480.81571060597696,278.89102585982465,447.11581721213884,335.3156288977577,407.8159560528643,381.40805865246233,365.1012204253732,417.1789802448813],"text":"eventA2B","callbacks":"{\"before\":[\"beforeA2B\"],\"after\":[\"afterA2B\"]}"}],"globalCallbacksText":"{\"enter\":[\"Genter\"],\"leave\":[\"Gleave\"],\"before\":[\"Gbefore\"],\"after\":[\"Gafter\"]}"}###
interface StateNameInterface {
    stateA: string;
    stateB: string;

}
interface EventNameInterface {
    eventA2B: string;

}
import StateMachine from "./StateMachine";
const { ccclass, property } = cc._decorator;

@ccclass
export class FsmClass extends cc.Component {

    private fsm: any;

    protected fsmTrigger(eventName: string, ...args: any[]) {
        this.fsm[eventName](...args);
    };

    protected fsmIs(stateName: string): boolean {
        return this.fsm.is(stateName);
    };

    protected fsmCan(eventName: string): boolean {
        return this.fsm.can(eventName);
    };

    protected fsmCannot(eventName: string): boolean {
        return this.fsm.cannot(eventName);
    };

    protected fsmCurrent(): string {
        return this.fsm.current;
    };

    protected fsmStartUp() {
        this.fsm = StateMachine.create({ "initial": "stateA", "events": [{ "name": "eventA2B", "from": "stateA", "to": "stateB" }], "callbacks": { "onenterstateA": [this.enterA, this.enterA2], "onleavestateA": [this.leaveA, this.leaveA2], "onenterstateB": [this.enterB], "onleavestateB": [this.leaveB], "onbeforeeventA2B": { "stateA": [this.beforeA2B] }, "onaftereventA2B": { "stateA": [this.afterA2B] }, "onenterstate": [this.Genter], "onleavestate": [this.Gleave], "onbeforeevent": [this.Gbefore], "onafterevent": [this.Gafter] } }, this);
    };

    public readonly stateName: StateNameInterface = {
        stateA: "stateA",
        stateB: "stateB"
    };

    public readonly eventName: EventNameInterface = {
        eventA2B: "eventA2B"
    };

    protected eventA2B(...args: any[]): void { this.fsm["eventA2B"](...args); };


    onLoad(){
        this.fsmStartUp();
        this.eventA2B("yes", 123);
    }

    protected enterA(eventName: string, from: string, to: string, ...args: any[]): void {
        console.warn("enterA", eventName, from, to, ...args);
    }
    protected enterA2(eventName: string, from: string, to: string, ...args: any[]): void {
        console.warn("enterA2", eventName, from, to, ...args);
    }
    protected leaveA(eventName: string, from: string, to: string, ...args: any[]): void {
        console.warn("leaveA", eventName, from, to, ...args);
    }
    protected leaveA2(eventName: string, from: string, to: string, ...args: any[]): void {
        console.warn("leaveA2", eventName, from, to, ...args);
    }
    protected enterB(eventName: string, from: string, to: string, ...args: any[]): void {
        console.warn("enterB", eventName, from, to, ...args);
    }
    protected leaveB(eventName: string, from: string, to: string, ...args: any[]): void {
        console.warn("leaveB", eventName, from, to, ...args);
    }
    protected beforeA2B(eventName: string, from: string, to: string, ...args: any[]): void {
        console.warn("beforeA2B", eventName, from, to, ...args);
    }
    protected afterA2B(eventName: string, from: string, to: string, ...args: any[]): void {
        console.warn("afterA2B", eventName, from, to, ...args);
    }
    protected Genter(eventName: string, from: string, to: string, ...args: any[]): void {
        console.warn("Genter", eventName, from, to, ...args);
    }
    protected Gleave(eventName: string, from: string, to: string, ...args: any[]): void {
        console.warn("Gleave", eventName, from, to, ...args);
    }
    protected Gbefore(eventName: string, from: string, to: string, ...args: any[]): void {
        console.warn("Gbefore", eventName, from, to, ...args);
    }
    protected Gafter(eventName: string, from: string, to: string, ...args: any[]): void {
        console.warn("Gafter", eventName, from, to, ...args);
    }

}
